package com.perfma.xtest.tool;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.util.NumberUtils;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.Map;

/**
 * @author: junhui.si
 * @date: 2023-11-13 18:11
 **/
public class SceneReportHourSectionExcelHelper2 {
//
//    private static final String TEMPLATE = "/Users/sijunhui/Downloads/word/scene_report_hour_section_template.xlsx";
//
//    public static void main(String[] args) {
//        aa();
//    }
//
//    private static List<SceneReportHourSectionModel1> listModel1() {
//        SceneReportHourSectionModel1 model1 = new SceneReportHourSectionModel1("1", "100", "10", "10%");
//        SceneReportHourSectionModel1 model2 = new SceneReportHourSectionModel1("2", "200", "20", "20%");
//        SceneReportHourSectionModel1 model3 = new SceneReportHourSectionModel1("3", "300", "30", "30%");
//        return Lists.newArrayList(model1, model2, model3);
//    }
//
//    private static List<SceneReportHourSectionModel2> listModel2() {
//        SceneReportHourSectionModel2 model1 = new SceneReportHourSectionModel2("综合资源", "ip1", "CPU", "{.machineCpuList_1}");
//        SceneReportHourSectionModel2 model2 = new SceneReportHourSectionModel2("综合资源", "ip1", "内存", "{.machineMemList_1}");
//        SceneReportHourSectionModel2 model3 = new SceneReportHourSectionModel2("综合资源", "ip1", "磁盘", "{.machineDiskList_1}");
//        SceneReportHourSectionModel2 model4 = new SceneReportHourSectionModel2("综合资源", "ip1", "网络", "{.machineNetList_1}");
//
//        SceneReportHourSectionModel2 model5 = new SceneReportHourSectionModel2("综合资源", "ip2", "CPU", "{.machineCpuList_2}");
//        SceneReportHourSectionModel2 model6 = new SceneReportHourSectionModel2("综合资源", "ip2", "内存", "{.machineMemList_2}");
//        SceneReportHourSectionModel2 model7 = new SceneReportHourSectionModel2("综合资源", "ip2", "磁盘", "{.machineDiskList_2}");
//        SceneReportHourSectionModel2 model8 = new SceneReportHourSectionModel2("综合资源", "ip2", "网络", "{.machineNetList_2}");
//        return Lists.newArrayList(model1, model2, model3, model4, model5, model6, model7, model8);
//    }
//
//    private static List<Map<String, String>> listModel3() {
//        Map<String, String> model1 = Maps.newHashMap();
//        model1.put("machineCpuList_1", "cpu1");
//        model1.put("machineMemList_1", "mem1");
//        model1.put("machineDiskList_1", "disk1");
//        model1.put("machineNetList_1", "net1");
//
//        model1.put("machineCpuList_2", "cpu2");
//        model1.put("machineMemList_2", "mem2");
//        model1.put("machineDiskList_2", "disk2");
//        model1.put("machineNetList_2", "net2");
//
//        Map<String, String> model2 = Maps.newHashMap();
//        model2.put("machineCpuList_1", "cpu1");
//        model2.put("machineMemList_1", "mem1");
//        model2.put("machineDiskList_1", "disk1");
//        model2.put("machineNetList_1", "net1");
//
//        model2.put("machineCpuList_2", "cpu2");
//        model2.put("machineMemList_2", "mem2");
//        model2.put("machineDiskList_2", "disk2");
//        model2.put("machineNetList_2", "net2");
//
//        Map<String, String> model3 = Maps.newHashMap();
//        model3.put("machineCpuList_1", "cpu1");
//        model3.put("machineMemList_1", "mem1");
//        model3.put("machineDiskList_1", "disk1");
//        model3.put("machineNetList_1", "net1");
//
//        model3.put("machineCpuList_2", "cpu2");
//        model3.put("machineMemList_2", "mem2");
//        model3.put("machineDiskList_2", "disk2");
//        model3.put("machineNetList_2", "net2");
//
//        return Lists.newArrayList(model1, model2, model3);
//    }
//
//    private static List<Map<String, String>> listName() {
//        Map<String, String> model1 = Maps.newHashMap();
//        model1.put("transactionNameList", "事务1");
//
//        Map<String, String> model2 = Maps.newHashMap();
//        model2.put("transactionNameList", "事务2");
//
//        Map<String, String> model3 = Maps.newHashMap();
//        model3.put("transactionNameList", "事务3");
//
//        return Lists.newArrayList(model1, model2, model3);
//    }
//
//    private static List<Map<String, String>> listTpsName() {
//        Map<String, String> model1 = Maps.newHashMap();
//        model1.put("tpsName", "TPS（笔/秒）");
//
//        Map<String, String> model2 = Maps.newHashMap();
//        model2.put("tpsName", "TPS（笔/秒）");
//
//        Map<String, String> model3 = Maps.newHashMap();
//        model3.put("tpsName", "TPS（笔/秒）");
//
//        return Lists.newArrayList(model1, model2, model3);
//    }
//
//    private static void aa() {
//        String fileName = "/Users/sijunhui/Downloads/word/complexFill_" + System.currentTimeMillis() + ".xlsx";
//        ExcelWriter excelWriter = null;
//        try {
//            excelWriter = EasyExcel.write(fileName)
//                    .autoCloseStream(Boolean.FALSE)
//                    // 行宽自适应
//                    .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
//                    // 样式
//                    .registerWriteHandler(getWriteHandler())
//                    // 单元格格式
//                    .registerWriteHandler(new CustomCellWriteHandler())
//                    // 忽略"以文本形式储存的数字"的告警检查
//                    .registerWriteHandler(new IgnoreTextSheetWriteHandler())
//                    .build();
//
//            // 根据数据生成sheet
//            generateSheet(excelWriter);
//
//
//        } finally {
//            if (excelWriter != null) {
//                excelWriter.finish();
//            }
//        }
//    }
//
//    private static void generateSheet(ExcelWriter excelWriter) {
//        WriteSheet writeSheet = EasyExcel.writerSheet(0).build();
//
//        // tableDataList中一个元素对应一个表格（table）
//        generateTable(excelWriter, writeSheet);
//    }
//    private static int generateTable(ExcelWriter excelWriter, WriteSheet writeSheet) {
////        // 表格名
////        String tableName = planReportTable.getTableName();
////        List<PlanReportTableRowData> planReportTableDataList = planReportTable.getDataList();
////        String planReportTableDataListStr = JSON.toJSONString(planReportTableDataList);
////        JSONArray dataListJson = JSON.parseArray(planReportTableDataListStr);
////
////        // 表头
////        LinkedHashMap<String, String> headMap = planReportTable.getTitleMap();
////        // key:字段名，value:行序号（相对）
////        Map<String, Integer> titleOrderMap = getTitleOrderMap(headMap);
////        Map<Integer, String> orderTitleMap = getOrderTitleMap(headMap);
////
////        WriteTable writeTable = EasyExcel.writerTable(tableIndex).needHead(Boolean.TRUE).build();
////        writeTable.set
////        // 设置表头
////        writeTable.setHead(getHead(tableName, Lists.newArrayList(headMap.values())));
////        // 设置合并单元格的规则
////        writeTable.setCustomWriteHandlerList(getMergeWriteHandlerList(titleOrderMap, orderTitleMap, headMap, dataListJson, tableRowOffset, tableColumnOffset));
////        if (tableIndex > 0) {
////            // 每个表格之间空出 {@link TABLE_INTERVAL_ROW_COUNT} 行
////            writeTable.setRelativeHeadRowIndex(TABLE_INTERVAL_ROW_COUNT);
////        }
////
////        // 写入数据
////        List<List<Object>> dataList = dataList(headMap, dataListJson);
////        excelWriter.write(dataList, writeSheet, writeTable);
////
////        return dataList.size() + TITLE_ROW_COUNT;
//        return -1;
//    }
//
//    private static WriteHandler getWriteHandler() {
//        WriteCellStyle commonStyle = new WriteCellStyle();
//        //设置底边框;
//        commonStyle.setBorderBottom(BorderStyle.THIN);
//        commonStyle.setBottomBorderColor((short) 0);
//        //设置左边框;
//        commonStyle.setBorderLeft(BorderStyle.THIN);
//        //设置左边框颜色;
//        commonStyle.setLeftBorderColor((short) 0);
//        //设置右边框;
//        commonStyle.setBorderRight(BorderStyle.THIN);
//        //设置右边框颜色;
//        commonStyle.setRightBorderColor((short) 0);
//        //设置顶边框;
//        commonStyle.setBorderTop(BorderStyle.THIN);
//        //设置顶边框颜色;
//        commonStyle.setTopBorderColor((short) 0);
//
//        // 表头的样式
//        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
//        BeanUtils.copyProperties(commonStyle, headWriteCellStyle);
//        WriteFont headWriteFont = new WriteFont();
//        headWriteFont.setFontHeightInPoints((short) 16);
//        headWriteFont.setFontName("宋体");
//        headWriteCellStyle.setWriteFont(headWriteFont);
//
//        // 内容的样式
//        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
//        BeanUtils.copyProperties(commonStyle, contentWriteCellStyle);
//        WriteFont contentWriteFont = new WriteFont();
//        contentWriteFont.setFontHeightInPoints((short) 14);
//        contentWriteFont.setFontName("宋体");
//        contentWriteCellStyle.setWriteFont(contentWriteFont);
//        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
//        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//        contentWriteCellStyle.setQuotePrefix(true);
//
//
//        return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
//    }
//
//    /**
//     * 单元格拦截器
//     */
//    static class CustomCellWriteHandler implements CellWriteHandler {
//
//        @Override
//        public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {
//
//        }
//
//        @Override
//        public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
//
//        }
//
//
//        @Override
//        public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
//            // 对单元格格式进行简单处理
//            if (!isHead) {
//                String cellValue = cell.getStringCellValue();
//                if (Strings.isNullOrEmpty(cellValue)) {
//                    return;
//                }
//
//                try {
//                    double valueDouble = NumberUtils.parseDouble(cellValue, null);
//                    cell.setCellType(CellType.NUMERIC);
//                    cell.setCellValue(valueDouble);
//                } catch (Exception e) {
//                    cell.setCellType(CellType.STRING);
//                    cell.setCellValue(cellValue);
//
//                    // 主机和应用数据汇总中，-100%的数据特殊处理
//                    if ("-100%".equals(cellValue)) {
//                        cell.setCellValue("-");
//                    }
//                }
//            }
//        }
//    }
}
